package org.douxiaoban.client.tab;

import java.util.ArrayList;

import org.douxiaoban.client.R;
import org.douxiaoban.client.adapter.DouGroupOneTopicAdapter;
import org.douxiaoban.client.api.DouGroupApi;
import org.douxiaoban.client.app.App;
import org.douxiaoban.client.widget.BaseTabView;
import org.douxiaoban.client.widget.ElasticListView;
import org.douxiaoban.data.DouGroupOneTopicData;
import org.douxiaoban.data.DouGroupOneTopicData.DouReplyItemData;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class DouGroupOneTopicTab extends BaseTabView{
	
	private View mHeaderView;
	private View mHeaderBody;
	private DouGroupOneTopicAdapter mReplyAdapter;
	private ElasticListView mListView;
	private DouGroupApi mDouApi;
	private DouGroupOneTopicData mOneTopicData;
	private ImageView mAvatarIv;
	private boolean mHasData;
	
	/*-第一页,完整的head,不是则只有一个标题-*/
	private boolean mIsFirstPage;
	//foot view
//	private View mFootView;
//	private ProgressBar mFootProgressbar;
//	private TextView mFootText;
	
	public DouGroupOneTopicTab(Context context){
		this(context, null);
	}
	
	public DouGroupOneTopicTab(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mListView = new ElasticListView(context, false);
		addToBodyView(mListView);
		LayoutInflater inflater = LayoutInflater.from(context);
		mHeaderView = inflater.inflate(R.layout.group_one_topic_info, null);
		mHeaderBody = mHeaderView.findViewById(R.id.topic_head_body);
		mListView.addHeaderView(mHeaderView);
		mListView.setBackgroundColor(Color.WHITE);
	}
	
	public void initView(DouGroupOneTopicData data){
		hideProgress();
		mHasData = true;
		mReplyAdapter = new DouGroupOneTopicAdapter(getContext(), data.mReplys);
		mListView.setAdapter(mReplyAdapter);
		initHeadView(data);
	}
	
	public boolean hasData(){
		return mHasData;
	}
	
	public DouGroupOneTopicAdapter getReplysAdapter(){
		return mReplyAdapter;
	}
	
	public ElasticListView getListView(){
		return mListView;
	}

	/**
	 * 不是回复列表第一页的时候, 头部只显示标题-
	 */
	public void setOnlyTitle(){
		mHeaderBody.setVisibility(View.GONE);
	}
	
	
	private void initHeadView(DouGroupOneTopicData data){
		TextView nameTv = (TextView)mHeaderView.findViewById(R.id.topic_name);
		TextView signatureTv = (TextView)mHeaderView.findViewById(R.id.topic_signature);
		TextView timeTv = (TextView)mHeaderView.findViewById(R.id.topic_time);
		TextView titleTv = (TextView)mHeaderView.findViewById(R.id.topic_title);
		TextView content = (TextView)mHeaderView.findViewById(R.id.topic_content);
		mHeaderView.findViewById(R.id.topic_reply).setVisibility(View.VISIBLE);
		if(mHeaderBody.getVisibility() == View.GONE){
			titleTv.setText(data.mTitle);
			return;
		}
		mAvatarIv = (ImageView)mHeaderView.findViewById(R.id.topic_avatar);
		App.getDouanApiInstance().reqBitmap(mHandler, data.mCurTopicInfo.mAvatarLink);
		nameTv.setText(data.mCurTopicInfo.mName);
		signatureTv.setText(data.mCurTopicInfo.mSignature);
		timeTv.setText(data.mCurTopicInfo.mTime);
		timeTv.setTextColor(getResources().getColor(R.color.green));
		titleTv.setVisibility(View.VISIBLE);
		titleTv.setText(data.mTitle);
		content.setText(Html.fromHtml(data.mCurTopicInfo.mContent));
		
	}
	
	
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			Bitmap bitmap = (Bitmap)msg.obj;
			mAvatarIv.setImageBitmap(bitmap);
		}
	};
	
	
}
